package com.zlq.javaWeb3.controller;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.util.Collection;
@WebServlet(urlPatterns = "/filesController")
@MultipartConfig(
        location = "D:\\tup"
)
public class FilesController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");

        Collection<Part> parts = req.getParts();
        if (parts != null && !parts.isEmpty()){
            for (Part part : parts){
                String submittedFileName = part.getSubmittedFileName();
//                String position = part.getHeader("content-disposition");
//                int i = position.indexOf("filename=");
//                String sub = position.substring(i + 10);
//                sub = sub.substring(0,sub.length()-1);
                if (submittedFileName != null && "".equals(submittedFileName)){
                    part.write(submittedFileName);
                    continue;

                }
            }

        }
    }
}
